﻿using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor.YeniYapi
{
    public partial class KoliUstuCCYeni : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet dsKoli;
        public KoliUstuCCYeni(DataSet dsKoli, string Path, bool KarbelOnFooter)
        {
            InitializeComponent();

            this.dsKoli = dsKoli;
            
            DataRelation FK_KoliUstu = new DataRelation("FK_KoliUstu",
              dsKoli.Tables["Table"].Columns["RowNr"],
              dsKoli.Tables["Table1"].Columns["RowNr"]);
            dsKoli.Relations.Add(FK_KoliUstu);

            xrTableCellKarbel.Visible = KarbelOnFooter;
            //foreach (XRTableCell tc in new XRTableCell[] { tcStyle,tcReferences})
            //    tc.BeforePrint += ReSize;
            tcMagaza.BeforePrint += ReSize2;

            BindDstoReport();

            ExportToPdf(Path);
        }

        private void BindDstoReport()
        {
            this.DataMember = "Table";
            this.DataSource = dsKoli;

            tcSezon.DataBindings.Add("Text", null, "Table.Sezon");
            tcMagaza.DataBindings.Add("Text", null, "Table.Magaza");
            tcBoxNr.DataBindings.Add("Text", null, "Table.BoxNr");
            tcTotal.DataBindings.Add("Text", null, "Table.Total");

            lbStyle.DataBindings.Add("Bookmark", null, "Table.Style");
            lbReference.DataBindings.Add("Bookmark", null, "Table.Reference");

            this.DetailReport.DataMember = "Table.FK_KoliUstu";
            this.DetailReport.DataSource = dsKoli;

            barcode.DataBindings.Add("Text", null, "Table.FK_KoliUstu.Barcode");
            tcStyle.DataBindings.Add("Text", null, "Table.FK_KoliUstu.Style");
            tcReferences.DataBindings.Add("Text", null, "Table.FK_KoliUstu.Reference");
            tcTailles.DataBindings.Add("Text", null, "Table.FK_KoliUstu.Tailles");
            tcPcs.DataBindings.Add("Text", null, "Table.FK_KoliUstu.Pcs");

            tcStyle.ProcessDuplicatesMode = DevExpress.XtraReports.UI.ProcessDuplicatesMode.Merge;
            tcReferences.ProcessDuplicatesMode = DevExpress.XtraReports.UI.ProcessDuplicatesMode.Merge;
        }

        private void ReSize(object sender, PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 14F;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }


        private void ReSize2(object sender, PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 42F;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void DetailReport_BeforePrint(object sender, PrintEventArgs e)
        {
            if (GetCurrentColumnValue("RowNr") != null)   //sonradan eklendi
            {
                GroupFooter1.HeightF = 215F;
                string RowNr = GetCurrentColumnValue("RowNr").ToString();
                int RowCount = dsKoli.Tables[1].Select("RowNr = " + RowNr).Length;
                float Height = (10 - RowCount) * 90 + 215;
                GroupFooter1.HeightF = Height;
            }
        }
    }
}
